package com.chinacache.maint.workorder.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.chinacache.maint.commons.orm.hibernate.Page;
import com.chinacache.maint.commons.web.struts2.CRUDActionSupport;
import com.chinacache.maint.workorder.entity.Field;
import com.chinacache.maint.workorder.entity.FieldAttribute;
import com.chinacache.maint.workorder.entity.FieldClass;
import com.chinacache.maint.workorder.entity.FieldClassToField;
import com.chinacache.maint.workorder.service.FieldAttributeManager;
import com.chinacache.maint.workorder.service.FieldClassManager;
import com.chinacache.maint.workorder.service.FieldClassToFieldManager;
import com.chinacache.maint.workorder.service.FieldManager;

@SuppressWarnings("serial")
public class FieldClassAction extends CRUDActionSupport<FieldClass> {
	private FieldClassManager fieldClassManager;
	private FieldManager fieldManager;
	private FieldClassToFieldManager fieldClassToFieldManager;
	private FieldAttributeManager fieldAttributeManager;
	private FieldClass entity;
	private Integer id;
	private Page<FieldClass> page = new Page<FieldClass>();
	private Page<Field> pageField = new Page<Field>();
	private List<Field> fields = new ArrayList<Field>();
	private Integer[] fieldId;
	private Integer fieldClassId;
	private Integer fctfId;
	Map<List, Map> view = null;

	// private String condition;

	public void setFctfId(Integer fctfId) {
		this.fctfId = fctfId;
	}

	public void setFieldId(Integer[] fieldId) {
		this.fieldId = fieldId;
	}

	@Override
	public String delete() throws Exception {
		fieldClassManager.deleteFieldClass(id);
		return RELOAD;
	}

	@Override
	public FieldClass getModel() {
		return entity;
	}

	@Override
	public Page<FieldClass> getPage() {
		return page;
	}

	@Override
	public String list() throws Exception {
		page = fieldClassManager.getAllFieldClass(page);
		return SUCCESS;
	}

	@Override
	public void prepare() throws Exception {
		if (id == null && fieldClassId != null)
			id = fieldClassId;
		if (id != null) {
			fieldClassId = id;
			entity = fieldClassManager.getFieldClass(id);
		} else {
			entity = new FieldClass();
		}
	}

	public String field() throws Exception {
		return "field";
	}

	public String deleteFctf() throws Exception {
		fieldClassToFieldManager.deleteFieldClassToField(fctfId);
		return "field";
	}

	public String showField() throws Exception {
		pageField = fieldManager.getAllField(pageField);
		return "showField";
	}

	public String addField() throws Exception {
		for (int i = 0; i < fieldId.length; i++) {
			FieldClassToField fieldClassToField = new FieldClassToField();
			Field field = fieldManager.getField(fieldId[i]);
			fieldClassToField.setField(field);
			fieldClassToField.setFieldClass(entity);
			fieldClassToFieldManager.saveFieldClassToField(fieldClassToField);
		}
		return field();
	}

	@Override
	public String save() throws Exception {
		fieldClassManager.saveFieldClass(entity);
		return RELOAD;
	}

	@SuppressWarnings("unchecked")
	public String view() throws Exception {
		Map<String, List> fas = new HashMap<String, List>();
		for (FieldClassToField fctField : entity.getFieldClassFields()) {
			List<List> fasList = new ArrayList<List>();
			view = new HashMap<List, Map>();
			Field field = fctField.getField();
			Set<FieldAttribute> faSet = field.getFieldAttibutes();
			List<Integer> groupIds = new ArrayList<Integer>();
			for (FieldAttribute fa : faSet) {
				if (!groupIds.contains(fa.getGroupId())) {
					groupIds.add(fa.getGroupId());
				}
			}
			for (Integer groupId : groupIds) {
				List faByGroupIdList = fieldAttributeManager
						.getFieldAttributeByGroupId(groupId);
				fasList.add(faByGroupIdList);
			}
			fas.put(field.getName(), fasList);
		}
		List<String> aList = new ArrayList<String>();
		aList.add(entity.getName());
		aList.add(entity.getMemo());
		aList.add("");
		view.put(aList, fas);
		return "view";
	}

	public void setFieldClassManager(FieldClassManager fieldClassManager) {
		this.fieldClassManager = fieldClassManager;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setFieldManager(FieldManager fieldManager) {
		this.fieldManager = fieldManager;
	}

	public Page<Field> getPageField() {
		return pageField;
	}

	public void setFieldClassToFieldManager(
			FieldClassToFieldManager fieldClassToFieldManager) {
		this.fieldClassToFieldManager = fieldClassToFieldManager;
	}

	public List<Field> getFields() {
		return fields;
	}

	public FieldClass getEntity() {
		return entity;
	}

	public Integer getFieldClassId() {
		return fieldClassId;
	}

	public void setFieldClassId(Integer fieldClassId) {
		this.fieldClassId = fieldClassId;
	}

	public Map<List, Map> getView() {
		return view;
	}

	public void setView(Map<List, Map> view) {
		this.view = view;
	}

	public void setFieldAttributeManager(
			FieldAttributeManager fieldAttributeManager) {
		this.fieldAttributeManager = fieldAttributeManager;
	}

	// public void setCondition(String condition) {
	// this.condition = condition;
	// }

}
